// 把函数赋值给变量
let fn = function () {
    console.log('Hello First-calss Function')
}
fn()

// // 一个示例
// const Blogcontroller = {
//     index (posts) {return View.index(posts)}
// }

// // 优化
// const Blog = {
//     index: View.index
// }

// 函数作为参数
// forEach
function forEach(array, fn) {
    for(let i = 0; i < array.length; i++) {
        fn(array[i])
    }
}

let a = forEach([1,2,3],function(item) {
    // console.log(item*item)
})

// filter
function filter (array, fn) {
    let results = []
    for(let i = 0; i < array.length; i++) {
        if(fn(array[i])) {
            results.push(array[i])
        }
    }
    return results
}

let aa = filter([1,2,3,4,5,6], function(item) {
    return item % 2 !== 0;
})
// console.log(aa)


//函数作为返回值
function makeFn() {
    let msg = "Hello World"
    return function() {
        console.log(msg)
    }
}
// console.log(makeFn()())

// once
function once (fn) {
    let done = false
    return function () {
        if(!done) {
            done = true
            // console.log(this===global)
            // console.log(arguments)
            return fn.apply(this, arguments)
        }
    }
}

let pay = once(function (money) {
    console.log(`支付：${money} Rmb`)
})
// console.log(pay(5))

// map
const map = (array, fn) => {
    let results = []
    for(const value of array) {
        results.push(fn(value))
    }
    return results
}

console.log(map([1,2,3,4],function(item) {
    return item*2
}))

// every
const every = (array, fn)=> {
    let result = true
    for(const value of array) {
        result = fn(value)
        if(!result) {
            break
        }
    }
    return result
}

console.log(every(['1','2','34'],(item)=> {
    return item.length > 0
}))

// some
const some = (array, fn) => {
    let result = false
    for(const value of array) {
        result = fn(value)
        if(result) {
            break
        }
    }
    return result
}

console.log(some([1,2,34],(item)=> {
    return item > 10
}))